//
//  LJMDiscoverCollectionViewCell.m
//  wind
//
//  Created by 李金明 on 16/1/11.
//  Copyright © 2016年 李金明. All rights reserved.
//

#import "LJMDiscoverCollectionViewCell.h"
#import "LJMLunbo_Model.h"
#import "LJMRink_Model.h"
#import "LJMListen_Model.h"
#import "LJMRankArr_TableViewCell.h"
#import "LJMListen_TableViewCell.h"
#import "LJM_HotSong_Model.h"
#import "CarouselView.h"
#import "LJM_HotSongs_TableViewCell.h"
#import "LJM_Memory_TableViewCell.h"
#import "LJM_Activity_TableViewCell.h"
#define TWOTABLEVIEWCELLHEIGHT 330 //第二个cell的高度
#define ONETABLEVIEWCELLHEIGHT 100 //第一个cell的高度
#define THREETABLEVIEWCELLHEIGHT 330 //第三个cell的高度
#define FORITABLEVIEWCELLHEIGHT 160//第四个cell的高度
#define FIRETABLEVIEWCELLHEIGHT 430 //第五个cell的高度
@interface LJMDiscoverCollectionViewCell () <UITableViewDataSource, UITableViewDelegate, RankArrChangeDelegate, ListenMoveChangeDelegate, HotSongArrChangeDelegate, MemoryArrChangeDelegate, ListenCellChangeDelegate, ActivityMoveChangeDelegate>
#pragma Activtiy的协议动作 cell
- (void)changeActivityCellVCUrl:(NSString *)str;
#pragma Activtiy的协议动作 更多
- (void)changeActivityMoveVCUrl:(NSString *)str;
#pragma Memory的协议方法
- (void)changeMemoryToSong_Root_sub:(LJM_HotSong_Model *)model;
#pragma HotSong的协议方法
- (void)changeHotSongVCUrl:(NSString *)str type:(NSString *)type;
- (void)changeHotSongToSong_Root_sub:(LJM_HotSong_Model *)model;
#pragma ListTableView的协议方法MOVE
- (void)changeMoveVCUrl:(NSString *)str;
#pragma ListTableView的协议方法cell点击
- (void)changeMoveVCInt:(NSInteger)n;
#pragma RankArr的协议方法
- (void)changeVCUrl:(NSString *)str type:(NSString *)type;

@property (nonatomic, retain) UITableView *tableV;

@property (nonatomic, retain) NSMutableArray *lunbo_Arr;//轮播图数据
@property (nonatomic, retain) NSMutableArray *rankArr;//排行数据
@property (nonatomic, retain) NSMutableArray *listenArr;//大家都在听的数据
@property (nonatomic, retain) CarouselView *carouselV;// 轮播图view
@property (nonatomic, retain) NSMutableArray *hotSongsArr;//热门歌单
@property (nonatomic, retain) NSMutableDictionary *memoryDic;//记忆中的数据库
@property (nonatomic, retain) NSMutableDictionary *activityDic;//活动的数据

@end

@implementation LJMDiscoverCollectionViewCell
- (void)dealloc
{
    self.delegate = nil;
    [_activityDic release];
    [_memoryDic release];
    [_hotSongsArr release];
    [_tableV release];
    [_lunbo_Arr release];
    [_listenArr release];
    [_rankArr release];
    [_discoverArr release];
    [_carouselV release];
    [super dealloc];
  
}
#pragma set方法
- (void)setDiscoverArr:(NSMutableArray *)discoverArr
{
    if (_discoverArr != discoverArr) {
        [_discoverArr release];
        _discoverArr = [discoverArr retain];
    }
    if (0 != discoverArr.count) {
        

    //获取轮播图数据
    NSDictionary *lunboDic = [discoverArr objectAtIndex:0];
    self.lunbo_Arr = [NSMutableArray array];
    for (NSMutableDictionary *tempDic in [lunboDic objectForKey:@"data"]) {
        LJMLunbo_Model *model = [[LJMLunbo_Model alloc] init];
        [model initWithDic:tempDic];
        model.value = [[tempDic objectForKey:@"action"] objectForKey:@"value"];
        [self.lunbo_Arr addObject:model];
        [model release];
    }
    //获取排行数据
    NSDictionary *rinkDic = [discoverArr objectAtIndex:1];
    self.rankArr = [NSMutableArray array];
    for (NSMutableDictionary *tempDic in [rinkDic objectForKey:@"data"]) {
        LJMRink_Model *model = [[LJMRink_Model alloc] init];
        [model initWithDic:tempDic];
        model.type = [[tempDic objectForKey:@"action"] objectForKey:@"type"];
        [self.rankArr addObject:model];
        [model release];
    }
    //大家都在听
    NSDictionary *listenDic = [discoverArr objectAtIndex:2];
    self.listenArr = [NSMutableArray array];
    NSDictionary *sonListenDic = [[listenDic objectForKey:@"data"] objectAtIndex:0];
    
    for (NSMutableDictionary *tempDic in [sonListenDic objectForKey:@"songs"]) {
        
        LJMListen_Model *model = [[LJMListen_Model alloc] init];
        [model initWithDic:tempDic];
        //如果有无损音乐


        if (nil != [tempDic objectForKey:@"llList"] && ![[tempDic objectForKey:@"llList"] isEqual:[NSNull null]]) {
          model.llList = [[[tempDic objectForKey:@"llList"] objectAtIndex:0] objectForKey:@"url"];  
        }
        

        //装入播放的音乐
        model.auditionListArr = [NSMutableArray arrayWithArray:[tempDic objectForKey:@"auditionList"]];
        //装入作者的信息
        model.singersArr = [NSMutableArray arrayWithArray:[tempDic objectForKey:@"singers"]];
        
        [self.listenArr addObject:model];
        [model release];
        
    }


    //热门歌单
   NSDictionary *hotSongDic = [discoverArr objectAtIndex:3];
    self.hotSongsArr = [NSMutableArray array];
    for (NSMutableDictionary *tempHotDic in [hotSongDic objectForKey:@"data"]) {
        LJM_HotSong_Model *model = [[LJM_HotSong_Model alloc] init];
        [model initWithDic:tempHotDic];
        model.value = [[tempHotDic objectForKey:@"action"] objectForKey:@"value"];
        [self.hotSongsArr addObject:model];
        [model release];
    }
       //记忆中的歌曲
       NSDictionary *memoryDic = [discoverArr objectAtIndex:4];
        NSMutableArray *memoryArr = [NSMutableArray array];
        for (NSMutableDictionary *memoryTempDic in [memoryDic objectForKey:@"data"]) {
            LJM_HotSong_Model *model = [[LJM_HotSong_Model alloc] init];
            [model initWithDic:memoryTempDic];
            model.value = [[memoryTempDic objectForKey:@"action"] objectForKey:@"value"];
            [memoryArr addObject:model];
            [model release];
        }
        self.memoryDic = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[memoryDic objectForKey:@"name"], memoryArr, nil] forKeys:[NSArray arrayWithObjects:@"name", @"data", nil]];
        
        //我是歌手
        NSDictionary *activityDic = [discoverArr objectAtIndex:5];
        NSMutableArray *activityArr = [NSMutableArray array];
        for (NSMutableDictionary *tempHotDic in [activityDic objectForKey:@"data"]) {
            LJM_HotSong_Model *model = [[LJM_HotSong_Model alloc] init];
            [model initWithDic:tempHotDic];
            model.value = [[tempHotDic objectForKey:@"action"] objectForKey:@"value"];
            [activityArr addObject:model];
            [model release];
        }
        self.activityDic = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[activityDic objectForKey:@"name"], activityArr, nil] forKeys:[NSArray arrayWithObjects:@"name", @"data", nil]];
        
    
    }

    
    // 轮播图
    [self.carouselV removeFromSuperview];
    self.carouselV = [CarouselView carouselViewWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height / 3.5)];
    NSMutableArray *arr = [NSMutableArray array];
    for (LJMLunbo_Model *model in self.lunbo_Arr) {
        [arr addObject: model.picUrl];
    }
    self.carouselV.picArr = arr;
    
    
    self.tableV.tableHeaderView = self.carouselV;
    [self.tableV reloadData];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatView];
    }
    return self;
}
- (void)creatView
{
    self.backgroundColor = [UIColor whiteColor];
    
    //添加tableView
    [self addTableView];
 
}
#pragma 添加tableView
- (void)addTableView
{

    self.tableV = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:self.tableV];
    [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.bottom.equalTo(self.mas_bottom).offset(-30);
    }];
}
#pragma tableView的协议方法
#pragma 数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}
#pragma cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.row) {
        static NSString *rankCell = @"rank";
     LJMRankArr_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:rankCell];
    if (nil == cell) {
        cell = [[[LJMRankArr_TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:rankCell] autorelease];
    }
        if (self.rankArr) {
         cell.dataArr = [NSMutableArray arrayWithArray:self.rankArr];   
        }
        
        cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    } else if (2 == indexPath.row) {
        static NSString *hotSongsCell = @"hotSongs";
        LJM_HotSongs_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:hotSongsCell];
        if (nil == cell) {
            cell = [[[LJM_HotSongs_TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:hotSongsCell] autorelease];
        }
        cell.delegate = self;
        cell.dataArr = self.hotSongsArr;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (3 == indexPath.row) {
        static NSString *memoryCell = @"memory";
        LJM_Memory_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:memoryCell];
        if (nil == cell) {
            cell = [[[LJM_Memory_TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:memoryCell] autorelease];
        }
        cell.delegate = self;
        cell.titleLab.text = [self.memoryDic objectForKey:@"name"];
        
        cell.dataArr = [self.memoryDic objectForKey:@"data"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (4 == indexPath.row) {
        static NSString *activityCell = @"activity";
        LJM_Activity_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:activityCell];
        if (nil == cell) {
            cell = [[[LJM_Activity_TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:activityCell] autorelease];
        }
        cell.MOVEDelegatae = self;
        cell.titleStr = [self.activityDic objectForKey:@"name"];
        cell.dataArr = [self.activityDic objectForKey:@"data"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }

    else {
        
       static NSString *listenCell = @"listen";
        LJMListen_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:listenCell];
        if (nil == cell) {
            cell = [[[LJMListen_TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:listenCell] autorelease];
        }
        cell.cellDelegate = self;
        cell.MOVEDelegatae = self;
        cell.dataArr = [NSMutableArray arrayWithArray:self.listenArr];
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    }
    

}
#pragma 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.row) {
        return ONETABLEVIEWCELLHEIGHT;
    } else if (2 == indexPath.row) {
        return THREETABLEVIEWCELLHEIGHT;
    } else if (3 == indexPath.row) {
        return FORITABLEVIEWCELLHEIGHT;
    }
    else if (4 == indexPath.row) {
        return FIRETABLEVIEWCELLHEIGHT;
    }
    else {
        return TWOTABLEVIEWCELLHEIGHT;
    }

}
#pragma RankArr的协议方法
- (void)changeVCUrl:(NSString *)str type:(NSString *)type
{
    [self.delegate changeVCUrl:str type:type];
}
#pragma HotSong的协议方法
- (void)changeHotSongVCUrl:(NSString *)str type:(NSString *)type
{
    [self.delegate changeVCUrl:str type:type];
}
#pragma HotSong的协议方法
- (void)changeHotSongToSong_Root_sub:(LJM_HotSong_Model *)model;
{
    [self.delegate changeHotSongToSong_Root_sub:model];
}
#pragma Memory的协议方法
- (void)changeMemoryToSong_Root_sub:(LJM_HotSong_Model *)model
{
    [self.delegate changeHotSongToSong_Root_sub:model];
}
#pragma ListTableView的协议方法MOVE
- (void)changeMoveVCUrl:(NSString *)str
{
    [self.delegate changeMoveVCUrl:str];
}
#pragma ListTableView的协议方法cell点击
- (void)changeMoveVCInt:(NSInteger)n
{
    [self.delegate changeCellVCUrl:n];
}
#pragma Activtiy的协议动作
- (void)changeActivityCellVCUrl:(NSString *)str
{
    [self.delegate changeActivityToWebView:str];
}
#pragma Activtiy的协议动作 更多
- (void)changeActivityMoveVCUrl:(NSString *)str
{

    [self.delegate changeActivityMoveToVC:str];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
